#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mythread.h"
#include <QThread>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //创建子线程对象
    QThread* t1 = new QThread(this);
    QThread* t2 = new QThread(this);
    QThread* t3 = new QThread(this);

    //创建任务类的对象
    Generate* generate = new Generate;
    BubbleSort* bubble_sort = new BubbleSort;
    QuickSort* quick_sort = new QuickSort;

    //将任务对象移动到某个子线程
    generate->moveToThread(t1);
    bubble_sort->moveToThread(t2);
    quick_sort->moveToThread(t3);


    connect(this , &MainWindow::starting, generate , &Generate::working);
    //启动子线程
    connect(ui->start, &QPushButton::clicked , this ,[=](){
        emit starting(10000);
        t1->start();
    });
    connect(generate,&Generate::sendArray,bubble_sort,&BubbleSort::working);
    connect(generate,&Generate::sendArray,quick_sort,&QuickSort::working);
    //接受子线程发送的数据
    connect(generate , &Generate::sendArray , this , [=](QVector<int> list){
       t2->start();
       t3->start();
       for (int i=0;i<list.size();++i){
           ui->rand_list->addItem(QString::number(list.at(i)));
       }
    });
    connect(bubble_sort , &BubbleSort::Finish_Array , this , [=](QVector<int> list){
       for (int i=0;i<list.size();++i){
           ui->bubbel_list->addItem(QString::number(list.at(i)));
       }
    });
    connect(quick_sort , &QuickSort::Finish_Array , this , [=](QVector<int> list){
       for (int i=0;i<list.size();++i){
           ui->quick_list->addItem(QString::number(list.at(i)));
       }
    });
    connect(this , &MainWindow::destroyed , this , [](){

    });
}

MainWindow::~MainWindow()
{
    delete ui;
}

